<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="链接 安装及使用安装1npm install typescript -g 或1yarn global add typescript 使用新建demo.ts： 12345function test() &amp;#123;    let web: string=&amp;quot;hello world&amp;quot;    console.log(web);&amp;#125;test(); 编译： 12tsc temo.t">
<meta property="og:type" content="article">
<meta property="og:title" content="TypeScript总结">
<meta property="og:url" content="http://yoursite.com/2020/09/01/TypeScript总结/index.html">
<meta property="og:site_name" content="春风自是人间客">
<meta property="og:description" content="链接 安装及使用安装1npm install typescript -g 或1yarn global add typescript 使用新建demo.ts： 12345function test() &amp;#123;    let web: string=&amp;quot;hello world&amp;quot;    console.log(web);&amp;#125;test(); 编译： 12tsc temo.t">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-11-01T03:02:14.556Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="TypeScript总结">
<meta name="twitter:description" content="链接 安装及使用安装1npm install typescript -g 或1yarn global add typescript 使用新建demo.ts： 12345function test() &amp;#123;    let web: string=&amp;quot;hello world&amp;quot;    console.log(web);&amp;#125;test(); 编译： 12tsc temo.t">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: "",
      labels: ""
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2020/09/01/TypeScript总结/">





  <title>TypeScript总结 | 春风自是人间客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">春风自是人间客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">是非成败转头空，青山依旧在，几度夕阳红。</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-program">
          <a href="/categories/program" rel="section">
            
            程序
          </a>
        </li>
      
        
        <li class="menu-item menu-item-reading">
          <a href="/categories/reading" rel="section">
            
            阅读
          </a>
        </li>
      
        
        <li class="menu-item menu-item-idea">
          <a href="/categories/idea" rel="section">
            
            想法
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
            关于我
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/09/01/TypeScript总结/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="彭丹丹">
      <meta itemprop="description" content>
      <meta itemprop="image" content="https://upload.jianshu.io/users/upload_avatars/830956/debe5156eed7?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="春风自是人间客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">TypeScript总结</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-09-01T18:50:17+08:00">
                2020-09-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/program/" itemprop="url" rel="index">
                    <span itemprop="name">program</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  4,468
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  22
                </span>
              
            </div>
          

          
          
        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><a href="https://jspang.com/detailed?id=63" target="_blank" rel="noopener">链接</a></p>
<h1 id="安装及使用"><a href="#安装及使用" class="headerlink" title="安装及使用"></a>安装及使用</h1><h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install typescript -g</span><br></pre></td></tr></table></figure>
<p>或<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn global add typescript</span><br></pre></td></tr></table></figure></p>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><p><strong>新建<code>demo.ts</code>：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function test() &#123;</span><br><span class="line">    let web: string=&quot;hello world&quot;</span><br><span class="line">    console.log(web);</span><br><span class="line">&#125;</span><br><span class="line">test();</span><br></pre></td></tr></table></figure>
<p><strong>编译：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">tsc temo.ts</span><br><span class="line">node temo.js</span><br></pre></td></tr></table></figure>
<p><strong>或</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -g ts-node</span><br></pre></td></tr></table></figure>
<p>安装成功后：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ts-node Demo1.ts</span><br></pre></td></tr></table></figure></p>
<h1 id="静态类型"><a href="#静态类型" class="headerlink" title="静态类型"></a>静态类型</h1><p>被定义后就不可改变该亦是的类型了。</p>
<p>js是弱类型语言，可以被改变。</p>
<h2 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">// 定义count是number类型</span><br><span class="line">const count : number = 1; </span><br><span class="line">count = &apos;hello&apos;; // 此时改变类型为string就会报错编译不通过</span><br></pre></td></tr></table></figure>
<h2 id="自定义静态类型"><a href="#自定义静态类型" class="headerlink" title="自定义静态类型"></a>自定义静态类型</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">interface XisoJieJie &#123;</span><br><span class="line">    uname: string,</span><br><span class="line">    age: number</span><br><span class="line">&#125;</span><br><span class="line">const xh : XisoJieJie = &#123;</span><br><span class="line">    uname: &apos;小红&apos;,</span><br><span class="line">    age: 15</span><br><span class="line">&#125;</span><br><span class="line">console.log(xh);</span><br></pre></td></tr></table></figure>
<h2 id="基础静态类型和对象类型"><a href="#基础静态类型和对象类型" class="headerlink" title="基础静态类型和对象类型"></a>基础静态类型和对象类型</h2><h3 id="静态类型-1"><a href="#静态类型-1" class="headerlink" title="静态类型"></a>静态类型</h3><p><code>null</code>,<code>undefinde</code>,<code>symbol</code>,<code>boolean</code>，<code>void</code>等。</p>
<h3 id="对象类型"><a href="#对象类型" class="headerlink" title="对象类型"></a>对象类型</h3><ul>
<li>对象类型</li>
<li>数组类型</li>
<li>类类型</li>
<li>函数类型</li>
</ul>
<p><strong>对象类型</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">const xiaoJieJie: &#123;</span><br><span class="line">    name: string,</span><br><span class="line">    age: number</span><br><span class="line">&#125; = &#123;</span><br><span class="line">    name: &apos;小明&apos;,</span><br><span class="line">    age: 14</span><br><span class="line">&#125;</span><br><span class="line">console.log(xiaoJieJie.name)</span><br></pre></td></tr></table></figure></p>
<p><strong>数组类型</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">const arr : number [] = [1,2,3];</span><br><span class="line">const xiaoJieJies : String [] = [&apos;小明&apos;,&apos;小红&apos;,&apos;小黄&apos;];</span><br><span class="line">console.log(xiaoJieJies)</span><br></pre></td></tr></table></figure></p>
<p><strong>类类型</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">class Person&#123;&#125;</span><br><span class="line">const xm : Person = new Person();</span><br><span class="line">console.log(xm);</span><br></pre></td></tr></table></figure></p>
<p><strong>函数类型</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">const j : () =&gt; string = ()=&gt; &#123;return &apos;小明&apos;&#125;;</span><br><span class="line">console.log(j);</span><br></pre></td></tr></table></figure></p>
<h1 id="类型注释和类型推断"><a href="#类型注释和类型推断" class="headerlink" title="类型注释和类型推断"></a>类型注释和类型推断</h1><h2 id="类型注释"><a href="#类型注释" class="headerlink" title="类型注释"></a>类型注释</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">let count : number; </span><br><span class="line">count = 123</span><br></pre></td></tr></table></figure>
<h2 id="类型推断"><a href="#类型推断" class="headerlink" title="类型推断"></a>类型推断</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">let countInference = 123</span><br></pre></td></tr></table></figure>
<p>这时编辑器和编译器都会推断出是number类型。</p>
<h1 id="函数参数和返回类型定义"><a href="#函数参数和返回类型定义" class="headerlink" title="函数参数和返回类型定义"></a>函数参数和返回类型定义</h1><h2 id="类型定义"><a href="#类型定义" class="headerlink" title="类型定义"></a>类型定义</h2><p><strong>无定义时：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function getTotal(one : number, two :number)&#123;</span><br><span class="line">    return one + two</span><br><span class="line">&#125;</span><br><span class="line">const total = getTotal(1,2)</span><br></pre></td></tr></table></figure>
<p>虽然能推断出是返回number，但这样不规范。</p>
<p><strong>定义时：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function getTotal(one : number, two :number) : number&#123;</span><br><span class="line">    return one + two</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const total = getTotal(1,2)</span><br></pre></td></tr></table></figure>
<p><strong>无需return时：void</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">function sayHello()&#123;</span><br><span class="line">	console.log(&apos;hello world!);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>异常或死循环：never返回值类型</strong></p>
<p>异常：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function errorFuntion() : never&#123; </span><br><span class="line">   throw new Error()</span><br><span class="line">   console.log(&apos;Hello World&apos;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>死循环：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function forNever() : never&#123;</span><br><span class="line">    while(true)&#123;&#125;</span><br><span class="line">    console.log(&apos;Hello JSPang&apos;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="函数参数为对象（解构）时"><a href="#函数参数为对象（解构）时" class="headerlink" title="函数参数为对象（解构）时"></a>函数参数为对象（解构）时</h2><p>TS函数参数解构：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">function add (&#123;one, two&#125; : &#123;one: number, two: number&#125;) &#123;</span><br><span class="line">    return one + two;</span><br><span class="line">&#125;</span><br><span class="line">const three = add(&#123;one:1,two:3&#125;);</span><br><span class="line"></span><br><span class="line">console.log(three); // 4</span><br></pre></td></tr></table></figure>
<p>ES6函数参数解构：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function add(&#123;x,y&#125; = &#123;&#125;)&#123;</span><br><span class="line">    return x+y;</span><br><span class="line">&#125;</span><br><span class="line">var sum = add(&#123;x:3,y:5&#125;);</span><br><span class="line">console.log(sum); // 8</span><br></pre></td></tr></table></figure>
<h1 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h1><h2 id="各种类型"><a href="#各种类型" class="headerlink" title="各种类型"></a>各种类型</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">const numberArr : number [] = [1,2,3];</span><br><span class="line"></span><br><span class="line">const stringArr : string [] = [&apos;a&apos;,&apos;b&apos;,&apos;c&apos;];</span><br><span class="line"></span><br><span class="line">const undefinedArr : undefined [] = [undefined, undefined];</span><br><span class="line"></span><br><span class="line">const emptyArr : null [] = [null,null];</span><br><span class="line"></span><br><span class="line">const arr : (number | string) [] = [1,&apos;a&apos;,1,2,&apos;b&apos;];</span><br><span class="line"></span><br><span class="line">// 类型别名</span><br><span class="line">type Lady = &#123;name: string, age: number&#125;;</span><br><span class="line">const xiaoJieJies: Lady[] = [&#123;</span><br><span class="line">    name: &apos;小明&apos;,age:14</span><br><span class="line">&#125;,&#123;</span><br><span class="line">    name: &apos;小红&apos;, age: 35</span><br><span class="line">&#125;]</span><br><span class="line"></span><br><span class="line">class Lady1  &#123;name: string;age: number&#125;;</span><br><span class="line">const xiaoJieJies1: Lady1[] = [&#123;</span><br><span class="line">    name: &apos;小明&apos;,age:14</span><br><span class="line">&#125;,&#123;</span><br><span class="line">    name: &apos;小红&apos;, age: 35</span><br><span class="line">&#125;]</span><br></pre></td></tr></table></figure>
<p><code>type</code>和<code>class</code>的区别是<code>class</code>会编译出来，<code>type</code>会忽略。</p>
<h1 id="元组"><a href="#元组" class="headerlink" title="元组"></a>元组</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// 无约束</span><br><span class="line">const arr1 : (number | string) [] = [1,&apos;a&apos;,1,2,&apos;b&apos;];</span><br><span class="line"></span><br><span class="line">// 有约束</span><br><span class="line">const arr2 : [string,string,number] = [&apos;a&apos;,&apos;b&apos;,3];</span><br></pre></td></tr></table></figure>
<h1 id="接口"><a href="#接口" class="headerlink" title="接口"></a>接口</h1><h2 id="定义接口"><a href="#定义接口" class="headerlink" title="定义接口"></a>定义接口</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">interface Girl &#123;</span><br><span class="line">    name: string;</span><br><span class="line">    age: number;</span><br><span class="line">    bust: number</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const getResume = (girl: Girl) =&gt; &#123;</span><br><span class="line">    console.log(girl.name);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const xh = &#123;</span><br><span class="line">    name: &apos;小红&apos;,</span><br><span class="line">    age: 19,</span><br><span class="line">    bust:38</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">getResume(xh);</span><br></pre></td></tr></table></figure>
<h2 id="接口-interface-和类型别名-type-的区别"><a href="#接口-interface-和类型别名-type-的区别" class="headerlink" title="接口(interface)和类型别名(type)的区别"></a>接口(<code>interface</code>)和类型别名(<code>type</code>)的区别</h2><ul>
<li>类型别名可以直接给类型，比如<code>string</code>，但接口必须给对象。</li>
</ul>
<h2 id="接口定义非必填值"><a href="#接口定义非必填值" class="headerlink" title="接口定义非必填值?:"></a>接口定义非必填值<code>?:</code></h2><p>语法：<code>?:</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">interface Girl &#123;</span><br><span class="line">    name: string;</span><br><span class="line">    age: number;</span><br><span class="line">    bust ?: number</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="接口允许加入任意值-anyname-string-any"><a href="#接口允许加入任意值-anyname-string-any" class="headerlink" title="接口允许加入任意值[anyname:string]:any;"></a>接口允许加入任意值<code>[anyname:string]:any;</code></h2><p>语法：<code>[anyname:string]:any;</code></p>
<p><code>string</code>的意思是对象名是string类型，<code>any</code>的意思是对象值是任意类型。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">interface Girl1 &#123;</span><br><span class="line">    name: string;</span><br><span class="line">    age: number;</span><br><span class="line">    bust ?: number;</span><br><span class="line">    [anyname:string]: any;</span><br><span class="line">&#125;</span><br><span class="line">const xh = &#123;</span><br><span class="line">    name: &apos;小红&apos;,</span><br><span class="line">    age: 19,</span><br><span class="line">    sex:&apos;女&apos;,</span><br><span class="line">    hobby:&apos;兴趣&apos;,</span><br><span class="line">    year:22</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="接口里定义方法"><a href="#接口里定义方法" class="headerlink" title="接口里定义方法"></a>接口里定义方法</h2><p>语法：<code>fun(): type</code></p>
<p>比如：<code>say(): string;</code>代表<code>say</code>方法返回<code>string</code>类型的值。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">interface Girl1 &#123;</span><br><span class="line">    name: string;</span><br><span class="line">    age: number;</span><br><span class="line">    bust ?: number;</span><br><span class="line">    [anyname:string]: any;</span><br><span class="line">    say(): string;</span><br><span class="line">&#125;</span><br><span class="line">const xh = &#123;</span><br><span class="line">    name: &apos;小红&apos;,</span><br><span class="line">    age: 19,</span><br><span class="line">    sex:&apos;女&apos;,</span><br><span class="line">    hobby:&apos;兴趣&apos;,</span><br><span class="line">    year:22,</span><br><span class="line">    say()&#123;</span><br><span class="line">        return &apos;hello World!&apos;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="类实现接口"><a href="#类实现接口" class="headerlink" title="类实现接口"></a>类实现接口</h2><p>类实现接口时必须把<strong>所有</strong>要必填的属性和方法实现，否则就会报错。</p>
<p>错误的：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">class XiaoJieJie implements Girl&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>正确的：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">class XiaoJieJie implements Girl&#123;</span><br><span class="line">    name = &apos;小红&apos;;</span><br><span class="line">    age = 12;</span><br><span class="line">    bust= 90;</span><br><span class="line">    say()&#123;</span><br><span class="line">        return &apos;你好&apos;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">console.log(XiaoJieJie)</span><br></pre></td></tr></table></figure></p>
<h2 id="接口继承接口"><a href="#接口继承接口" class="headerlink" title="接口继承接口"></a>接口继承接口</h2><p>重点：</p>
<ul>
<li><code>interface...extends</code>；</li>
<li><code>function</code>里面的约束定型改为新接口名；</li>
<li>新的对象里面除了原接口约束的属性，也要把新的属性加上。</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">interface Teacher extends Girl &#123;</span><br><span class="line">    teach():string;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const getResume2 = (girl: Teacher) =&gt; &#123;</span><br><span class="line">    ...</span><br><span class="line">    girl.teach();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const girl2 = &#123;</span><br><span class="line">    ... // girl的所有属性</span><br><span class="line">    teach()&#123;</span><br><span class="line">        return &apos;hello&apos;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">getResume2(girl2);</span><br></pre></td></tr></table></figure>
<h2 id="自我总结"><a href="#自我总结" class="headerlink" title="自我总结"></a>自我总结</h2><p>接口更像是一种约束，约束用户的对象的属性类型。</p>
<h1 id="类"><a href="#类" class="headerlink" title="类"></a>类</h1><p>类描述对象的属性和方法。接口则包含类要实现的方法。</p>
<h2 id="继承与重写"><a href="#继承与重写" class="headerlink" title="继承与重写"></a>继承与重写</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">class Lady &#123;</span><br><span class="line">    content = &apos;hello world!&apos;;</span><br><span class="line">    sayHello () &#123;</span><br><span class="line">        return this.content</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class XiaoJieJie extends Lady &#123;</span><br><span class="line">    sayLove () &#123;</span><br><span class="line">        return &apos;I love you.&apos;</span><br><span class="line">    &#125;;</span><br><span class="line">    sayHello () &#123;</span><br><span class="line">        return super.sayHello() + &apos; 你好！&apos;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>JAVA中类的继承</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">class Animal&#123;</span><br><span class="line">    public void move()&#123;</span><br><span class="line">        System.out.println(&quot;动物可以移动&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">  </span><br><span class="line">class Dog extends Animal&#123;</span><br><span class="line">    @Override // 表示方法重写</span><br><span class="line">    public void move()&#123;</span><br><span class="line">        super.move(); // 引用父类的方法</span><br><span class="line">        System.out.println(&quot;狗可以跑和走&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>区别：</p>
<ul>
<li>java中要用到<code>@Override</code>标签</li>
</ul>
<p><strong>@Override标签的作用与好处</strong></p>
<ul>
<li>它是伪代码，表示方法重写</li>
<li>作为注释，帮助检查是否正确复写了父类中已有的方法</li>
<li>编译器可以验证该方法是否在父类中已有</li>
<li>非必填。如果没写@Override，方法名与父类一模一样，如果返回类型相同，视为重写，反之，视为新方法</li>
</ul>
<h2 id="访问类型"><a href="#访问类型" class="headerlink" title="访问类型"></a>访问类型</h2><h3 id="public"><a href="#public" class="headerlink" title="public"></a>public</h3><p>默认就是它</p>
<h3 id="private"><a href="#private" class="headerlink" title="private"></a>private</h3><p>私有，只有自己能用。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">class Lady &#123;</span><br><span class="line">    private content = &apos;hello world!&apos;;</span><br><span class="line">    private privateFn () &#123;</span><br><span class="line">        return &apos;private&apos; + this.content; // 能用</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line">class XiaoJieJie extends Lady &#123;</span><br><span class="line">    privateFn() &#123; // 编译不通过</span><br><span class="line">        return &apos;e&apos;;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line">console.log(new Lady().privateFn()); // 编译不通过</span><br></pre></td></tr></table></figure>
<h3 id="protected"><a href="#protected" class="headerlink" title="protected"></a>protected</h3><p>受保护，只能在自己及子类用。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">class Lady &#123;</span><br><span class="line">    protected protectedFn () &#123;</span><br><span class="line">        return &apos;protected&apos;;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class XiaoJieJie extends Lady &#123;</span><br><span class="line">    protectedFn () &#123;</span><br><span class="line">        return &apos;xj&apos;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">console.log(new Lady().protectedFn()); // 编译不通过</span><br></pre></td></tr></table></figure>
<h3 id="对比表"><a href="#对比表" class="headerlink" title="对比表"></a>对比表</h3><table>
<thead>
<tr>
<th style="text-align:center">类型</th>
<th style="text-align:center">public</th>
<th style="text-align:center">protected</th>
<th style="text-align:center">private</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">自己（父类）</td>
<td style="text-align:center">✓</td>
<td style="text-align:center">✓</td>
<td style="text-align:center">✓</td>
</tr>
<tr>
<td style="text-align:center">继承（子类）</td>
<td style="text-align:center">✓</td>
<td style="text-align:center">×</td>
<td style="text-align:center">×</td>
</tr>
<tr>
<td style="text-align:center">外部（<code>new Class().fun()</code>）</td>
<td style="text-align:center">✓</td>
<td style="text-align:center">✓</td>
<td style="text-align:center">×</td>
</tr>
</tbody>
</table>
<p>附：JAVA异同表</p>
<table>
<thead>
<tr>
<th style="text-align:center">类型</th>
<th style="text-align:center">public</th>
<th style="text-align:center">protected</th>
<th style="text-align:center">private</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">自己（父类）</td>
<td style="text-align:center">✓</td>
<td style="text-align:center">✓</td>
<td style="text-align:center">✓</td>
</tr>
<tr>
<td style="text-align:center">继承（子类）</td>
<td style="text-align:center">✓</td>
<td style="text-align:center">✓</td>
<td style="text-align:center">×</td>
</tr>
<tr>
<td style="text-align:center">外部（<code>new Class().fun()</code>）</td>
<td style="text-align:center">✓</td>
<td style="text-align:center">×</td>
<td style="text-align:center">×</td>
</tr>
</tbody>
</table>
<h2 id="构造函数"><a href="#构造函数" class="headerlink" title="构造函数"></a>构造函数</h2><h3 id="构造函数及继承"><a href="#构造函数及继承" class="headerlink" title="构造函数及继承"></a>构造函数及继承</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">class People &#123;</span><br><span class="line">    constructor (public name: string,public sex:string) &#123;</span><br><span class="line">        this.name = name;</span><br><span class="line">        this.sex = sex;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Teacher extends People &#123;</span><br><span class="line">    constructor (public age:number)&#123;</span><br><span class="line">        super(&apos;小明&apos;,&apos;女&apos;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">var people = new People(&apos;小红&apos;,&apos;女&apos;);</span><br><span class="line">console.log(people.name)</span><br><span class="line"></span><br><span class="line">var teacher = new Teacher(19);</span><br><span class="line">console.log(teacher,teacher.age);</span><br></pre></td></tr></table></figure>
<p>ES6实现对比：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">class People &#123;</span><br><span class="line">    constructor (name,sex) &#123;</span><br><span class="line">        this.name = name;</span><br><span class="line">        this.sex = sex;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Teacher extends People &#123;</span><br><span class="line">    constructor (name,sex,age)&#123;</span><br><span class="line">        super(name,sex);</span><br><span class="line">        this.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">var people = new People(&apos;小红&apos;,&apos;女&apos;);</span><br><span class="line">console.log(people.name)</span><br><span class="line"></span><br><span class="line">var teacher = new Teacher(&apos;小明&apos;,&apos;女&apos;,19);</span><br><span class="line">console.log(teacher,teacher.age);</span><br></pre></td></tr></table></figure></p>
<p>JAVA实现对比：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">class People &#123;</span><br><span class="line">    public String name;</span><br><span class="line">    public int sex;</span><br><span class="line">    public People (String name)&#123;System.out.println(name)&#125;;</span><br><span class="line">    public People (int sex )&#123;&#125;;</span><br><span class="line">    public People (Stirng name, int sex)&#123;&#125;</span><br><span class="line">    public People ()&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public class Teacher extends People &#123;</span><br><span class="line">...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="get和set"><a href="#get和set" class="headerlink" title="get和set"></a>get和set</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">class XiaoJieJie &#123;</span><br><span class="line">    constructor(private _age:number,private _name: string)&#123;</span><br><span class="line">    &#125;</span><br><span class="line">    get age()&#123;</span><br><span class="line">        return this._age-2;</span><br><span class="line">    &#125;;</span><br><span class="line">    set age(age:number)&#123;</span><br><span class="line">        this._age = age;</span><br><span class="line">    &#125;</span><br><span class="line">    get name()&#123;</span><br><span class="line">        return this._name+&apos;人&apos;;</span><br><span class="line">    &#125;;</span><br><span class="line">    set name(name:string)&#123;</span><br><span class="line">        this._name = name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const xj =  new XiaoJieJie(18,&apos;小明&apos;);</span><br><span class="line">console.log(xj,xj.age); // XiaoJieJie &#123; _age: 18, _name: &apos;小明&apos; &#125; 16</span><br></pre></td></tr></table></figure>
<p>编译如果无法通过需要加上es5：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">tsc demo.ts -t es5</span><br><span class="line">node demo.js</span><br></pre></td></tr></table></figure>
<p>java中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">class Stutent1&#123;</span><br><span class="line">    private String name;</span><br><span class="line">    private int age;</span><br><span class="line">    public String getName() &#123;</span><br><span class="line">        return name;</span><br><span class="line">    &#125;</span><br><span class="line">    public void setName(String name) &#123;</span><br><span class="line">        this.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">    public int getAge() &#123;</span><br><span class="line">        return age;</span><br><span class="line">    &#125;</span><br><span class="line">    public void setAge(int age) &#123;</span><br><span class="line">        this.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">    public void showStu1()&#123;</span><br><span class="line">        System.out.println(&quot;学生叫做&quot;+name+&quot;,年龄&quot;+age);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="static装饰符"><a href="#static装饰符" class="headerlink" title="static装饰符"></a>static装饰符</h2><p>不用new新建就可以创建</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">class Girl &#123;</span><br><span class="line">    static sayLove()&#123;</span><br><span class="line">        return &apos;你好&apos;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">console.log(Girl.sayLove());</span><br></pre></td></tr></table></figure>
<p>不用new Girl()就可以调用方法了。</p>
<h2 id="只读属性readonly"><a href="#只读属性readonly" class="headerlink" title="只读属性readonly"></a>只读属性readonly</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">class XiaoJieJie &#123;</span><br><span class="line">    constructor(private readonly _age:number,private _name: string)&#123;</span><br><span class="line">    &#125;</span><br><span class="line">    get age()&#123;</span><br><span class="line">        return this._age-2;</span><br><span class="line">    &#125;</span><br><span class="line">    set age(age:number)&#123; // 报错</span><br><span class="line">        this._age = age;</span><br><span class="line">    &#125;</span><br><span class="line">    get name()&#123;</span><br><span class="line">        return this._name+&apos;人&apos;;</span><br><span class="line">    &#125;;</span><br><span class="line">    set name(name:string)&#123;</span><br><span class="line">        this._name = name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const xj =  new XiaoJieJie(18,&apos;小明&apos;);</span><br><span class="line">xj.name = &apos;小红&apos;;</span><br><span class="line">xj.age = 33; // 报错</span><br><span class="line">console.log(xj,xj.age);</span><br></pre></td></tr></table></figure>
<h2 id="抽象类"><a href="#抽象类" class="headerlink" title="抽象类"></a>抽象类</h2><h3 id="不加abstract时"><a href="#不加abstract时" class="headerlink" title="不加abstract时"></a>不加abstract时</h3><p>特点：</p>
<ul>
<li>普通的类继承</li>
<li>普通的重写</li>
<li>可以调用父类</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">class Girl &#123;</span><br><span class="line">    skill()&#123;</span><br><span class="line">        conso.log(&apos;skill)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Girl1 extends Girl &#123;</span><br><span class="line">    skill()&#123;</span><br><span class="line">        console.log(&apos;cooking&apos;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Girl2 extends Girl &#123;</span><br><span class="line">    skill()&#123;</span><br><span class="line">        console.log(&apos;make cloths&apos;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Girl3 extends Girl &#123;</span><br><span class="line">    skill()&#123;</span><br><span class="line">        console.log(&apos;do gardening&apos;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">console.log(new Girl1().skill())</span><br><span class="line">console.log(new Girl2().skill())</span><br><span class="line">console.log(new Girl3().skill())</span><br><span class="line">console.log(new Girl().skill())</span><br></pre></td></tr></table></figure>
<h3 id="加abstract关键字时"><a href="#加abstract关键字时" class="headerlink" title="加abstract关键字时"></a>加abstract关键字时</h3><p>特点：</p>
<ul>
<li>不能调用父类</li>
<li>abstract方法不能有具体实现内容</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">abstract class Girl &#123;</span><br><span class="line">    abstract skill()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Girl1 extends Girl &#123;</span><br><span class="line">    skill()&#123;</span><br><span class="line">        console.log(&apos;cooking&apos;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Girl2 extends Girl &#123;</span><br><span class="line">    skill()&#123;</span><br><span class="line">        console.log(&apos;make cloths&apos;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Girl3 extends Girl &#123;</span><br><span class="line">    skill()&#123;</span><br><span class="line">        console.log(&apos;do gardening&apos;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">console.log(new Girl1().skill())</span><br><span class="line">console.log(new Girl2().skill())</span><br><span class="line">console.log(new Girl3().skill())</span><br></pre></td></tr></table></figure>
<p>区别：写了<code>abstract</code>关键字就是抽象类，如果不加，就是<code>重写（Override）</code>，也视为多态。</p>
<h3 id="多态"><a href="#多态" class="headerlink" title="多态"></a>多态</h3><p>多态是同一个行为具有多个不同表现形式或形态的能力。<br>比如打印机有打印方法，彩色打印机类打印方法是彩色，黑色打印机类打印方法是黑色。</p>
<p>java中多态的实现方式</p>
<ul>
<li>方式一：重写</li>
<li>方式二：接口</li>
<li>方式三：抽象类和抽象方法</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">abstract class Animal &#123;  </span><br><span class="line">    abstract void eat();  </span><br><span class="line">&#125;  </span><br><span class="line">  </span><br><span class="line">class Cat extends Animal &#123;  </span><br><span class="line">    public void eat() &#123;  </span><br><span class="line">        System.out.println(&quot;吃鱼&quot;);  </span><br><span class="line">    &#125;  </span><br><span class="line">    public void work() &#123;  </span><br><span class="line">        System.out.println(&quot;抓老鼠&quot;);  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;  </span><br><span class="line">  </span><br><span class="line">class Dog extends Animal &#123;  </span><br><span class="line">    public void eat() &#123;  </span><br><span class="line">        System.out.println(&quot;吃骨头&quot;);  </span><br><span class="line">    &#125;  </span><br><span class="line">    public void work() &#123;  </span><br><span class="line">        System.out.println(&quot;看家&quot;);  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>其它：写了<code>abstract</code>关键字就是抽象类，如果不加，就是<code>重写（Override）</code>，也视为多态。</p>
<h1 id="配置项tsconfig-json"><a href="#配置项tsconfig-json" class="headerlink" title="配置项tsconfig.json"></a>配置项tsconfig.json</h1><p><code>tsconfig.json</code>为配置文件，该配置文件通过<code>tsc --init</code>命令行来生成。</p>
<h2 id="include、exclude、files"><a href="#include、exclude、files" class="headerlink" title="include、exclude、files"></a>include、exclude、files</h2><p>直接在命令行输入<code>tsc</code>不带任何文件的话会默认编译所有文件。</p>
<p><code>include</code>属性是用来指定要编译的文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;include&quot;:[&quot;demo.ts&quot;],</span><br><span class="line">  &quot;compilerOptions&quot;: &#123;</span><br><span class="line">      //any something</span><br><span class="line">      //........</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>exclude</code>属性是用来指定不要编译的文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;exclude&quot;:[&quot;demo.ts&quot;],</span><br><span class="line">  &quot;compilerOptions&quot;: &#123;</span><br><span class="line">      //any something</span><br><span class="line">      //........</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>files</code>属性是用来指定要编译的文件，和<code>include</code>相同</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;files&quot;:[&quot;demo.ts&quot;],</span><br><span class="line">  &quot;compilerOptions&quot;: &#123;</span><br><span class="line">      //any something</span><br><span class="line">      //........</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="compilerOptions配置"><a href="#compilerOptions配置" class="headerlink" title="compilerOptions配置"></a>compilerOptions配置</h2><ul>
<li><code>removeComments</code>：编译文件是否显示注释</li>
<li><code>static</code>：是否按严格模式编译和书写</li>
<li><code>nolmplicitAny</code>：是否允许注解类型any不用声明</li>
<li><code>strictNullChecks</code>：是否允许非空检查</li>
<li><code>rootDir</code>：项目文件夹</li>
<li><code>outDir</code>：编译文件夹</li>
</ul>
<h1 id="联合类型和类型保护"><a href="#联合类型和类型保护" class="headerlink" title="联合类型和类型保护"></a>联合类型和类型保护</h1><h2 id="联合类型"><a href="#联合类型" class="headerlink" title="联合类型"></a>联合类型</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">interface Waiter &#123;</span><br><span class="line">    anjiao: boolean,</span><br><span class="line">    say:()=&gt;&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface Teacher &#123;</span><br><span class="line">    anjiao: boolean,</span><br><span class="line">    skill:()=&gt;&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function test(animal: Waiter | Teacher)&#123;&#125;)</span><br></pre></td></tr></table></figure>
<p>上例就是联合类型，<code>function test(animal: Waiter | Teacher){animal.say()})</code>会报错，所以需要类型保护。</p>
<h2 id="类型保护-类型断言"><a href="#类型保护-类型断言" class="headerlink" title="类型保护-类型断言"></a>类型保护-类型断言</h2><p>根据具体的值来断言用哪个接口</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">function test(animal: Waiter | Teacher)&#123;</span><br><span class="line">    // 方法一：类型断言：根据某个值来判断用哪个方法</span><br><span class="line">    if(animal.anjiao)&#123;</span><br><span class="line">        console.log((animal as Waiter).say());</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        console.log((animal as Teacher).skill());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="类型保护-in语法"><a href="#类型保护-in语法" class="headerlink" title="类型保护-in语法"></a>类型保护-in语法</h2><p>判断在对象中是否存在该方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">function test(animal: Waiter | Teacher)&#123;</span><br><span class="line">    // 方法二：in语法：判断在对象中是否存在该方法</span><br><span class="line">    if(&apos;skill&apos; in animal)&#123;</span><br><span class="line">        console.log(animal.skill());</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        console.log(animal.say());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="断言和in的实例"><a href="#断言和in的实例" class="headerlink" title="断言和in的实例"></a>断言和in的实例</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">interface Waiter &#123;</span><br><span class="line">    anjiao: boolean,</span><br><span class="line">    say:()=&gt;&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface Teacher &#123;</span><br><span class="line">    anjiao: boolean,</span><br><span class="line">    skill:()=&gt;&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function test(animal: Waiter | Teacher)&#123;</span><br><span class="line">    // 方法一：类型断言：根据某个值来判断用哪个方法</span><br><span class="line">    if(animal.anjiao)&#123;</span><br><span class="line">        console.log((animal as Waiter).say());</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        console.log((animal as Teacher).skill());</span><br><span class="line">    &#125;</span><br><span class="line">    // 方法二：in语法：判断在对象中是否存在该方法</span><br><span class="line">    if(&apos;skill&apos; in animal)&#123;</span><br><span class="line">        console.log(animal.skill());</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        console.log(animal.say());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const xiaohong : Teacher = &#123;</span><br><span class="line">    anjiao: false,</span><br><span class="line">    skill()&#123;</span><br><span class="line">        return &apos;你好&apos;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">const xiaoming : Waiter = &#123;</span><br><span class="line">    anjiao: true,</span><br><span class="line">    say()&#123;</span><br><span class="line">        return &apos;hello&apos;;</span><br><span class="line">    &#125;,</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">test(xiaohong);</span><br><span class="line">test(xiaoming);</span><br></pre></td></tr></table></figure>
<h2 id="类型保护-typeof语法"><a href="#类型保护-typeof语法" class="headerlink" title="类型保护-typeof语法"></a>类型保护-typeof语法</h2><p>判断是不是不同的类型</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">function add(first: string | number, second: string | number)&#123;</span><br><span class="line">    if(typeof first == &apos;string&apos; || typeof second == &apos;string&apos;)&#123;</span><br><span class="line">        return `$&#123;first&#125;$&#123;second&#125;`;</span><br><span class="line">    &#125;</span><br><span class="line">    return first + second;</span><br><span class="line">&#125;</span><br><span class="line">console.log(add(1,&apos;2&apos;)) // 12</span><br><span class="line">console.log(add(1,2)) // 3</span><br></pre></td></tr></table></figure>
<h2 id="类型保护-instanceof语法"><a href="#类型保护-instanceof语法" class="headerlink" title="类型保护-instanceof语法"></a>类型保护-instanceof语法</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">class NumberObj&#123;</span><br><span class="line">    count: number</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function addObj(first: object , second: object | NumberObj) &#123;</span><br><span class="line">    if (first instanceof NumberObj &amp;&amp; second instanceof NumberObj) &#123;</span><br><span class="line">        return first.count + second.count;</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 当是普通object时</span><br><span class="line">const obj1 : &#123;</span><br><span class="line">    count: number</span><br><span class="line">&#125; = &#123;</span><br><span class="line">    count: 1</span><br><span class="line">&#125;</span><br><span class="line">const obj2 : &#123;</span><br><span class="line">    count: number</span><br><span class="line">&#125; = &#123;</span><br><span class="line">    count: 3</span><br><span class="line">&#125;</span><br><span class="line">console.log(addObj(obj1,obj2)) // 0</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">// 当是NumberObj对象时</span><br><span class="line">const obj3 = new NumberObj();</span><br><span class="line">obj3.count = 45;</span><br><span class="line">const obj4 = new NumberObj();</span><br><span class="line">obj4.count = 435;</span><br><span class="line">console.log(addObj(obj3,obj4)) // 480</span><br></pre></td></tr></table></figure>
<h1 id="枚举"><a href="#枚举" class="headerlink" title="枚举"></a>枚举</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">enum Data &#123;</span><br><span class="line">    MASSAGE,</span><br><span class="line">    STATU,</span><br><span class="line">    DATA</span><br><span class="line">&#125;</span><br><span class="line">console.log(Data[1],Data.DATA,Data)</span><br></pre></td></tr></table></figure>
<p>打印出来是：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">STATU</span><br><span class="line">2</span><br><span class="line">&#123;</span><br><span class="line">  &apos;0&apos;: &apos;MASSAGE&apos;,</span><br><span class="line">  &apos;1&apos;: &apos;STATU&apos;,</span><br><span class="line">  &apos;2&apos;: &apos;DATA&apos;,</span><br><span class="line">  MASSAGE: 0,</span><br><span class="line">  STATU: 1,</span><br><span class="line">  DATA: 2</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>即<code>Data[1]</code>不是第<code>2</code>个，而是属性为<code>1</code>的的值。</p>
<p>枚举可以自定义值：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">enum Data &#123;</span><br><span class="line">    MASSAGE = &apos;success&apos;,</span><br><span class="line">    STATU = 3,</span><br><span class="line">    DATA</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>打印：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">undefined</span><br><span class="line">4 </span><br><span class="line">&#123; &apos;3&apos;: &apos;STATU&apos;, &apos;4&apos;: &apos;DATA&apos;, MASSAGE: &apos;success&apos;, STATU: 3, DATA: 4 &#125;</span><br></pre></td></tr></table></figure>
<p>如果是数字，会累加。</p>
<p>枚举可实现的，对象也可以实现，但是它可以实现<code>反射映射</code>。</p>
<h1 id="泛型"><a href="#泛型" class="headerlink" title="泛型"></a>泛型</h1><h2 id="在方法中使用"><a href="#在方法中使用" class="headerlink" title="在方法中使用"></a>在方法中使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function join&lt;T&gt;(first: T, second: T)&#123;</span><br><span class="line">    return `$&#123;first&#125;$&#123;second&#125;`;</span><br><span class="line">&#125;</span><br><span class="line">console.log(join &lt;string&gt; (&apos;peng&apos;,&apos;dan&apos;)) // &apos;pengdan&apos;</span><br><span class="line">console.log(join &lt;number&gt; (3,2)) // 32</span><br></pre></td></tr></table></figure>
<p>参数1和参数2可以是任意类型，但2者得同时是同一类型。</p>
<p>优化：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function join&lt;T1,T2&gt;(first: T1, second: T2)&#123;</span><br><span class="line">    return `$&#123;first&#125;$&#123;second&#125;`;</span><br><span class="line">&#125;</span><br><span class="line">console.log(join &lt;number,string&gt; (3,&apos;date&apos;)) // 3date</span><br></pre></td></tr></table></figure>
<h2 id="在类中使用"><a href="#在类中使用" class="headerlink" title="在类中使用"></a>在类中使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">class SelectGirl &#123;</span><br><span class="line">    constructor (private girls: string)&#123;&#125;</span><br><span class="line">    getGirl(index: number): string &#123;</span><br><span class="line">        return this.girls[index];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">const selectGirl = new SelectGirl([&apos;小A&apos;,&apos;小B&apos;,&apos;小C&apos;]);</span><br></pre></td></tr></table></figure>
<p>这时只能使用string类型的，改为泛型即可使用任意类型。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">class SelectGirl&lt;T&gt; &#123;</span><br><span class="line">    constructor (private girls: T)&#123;&#125;</span><br><span class="line">    getGirl(index: number): T &#123;</span><br><span class="line">        return this.girls[index];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">const selectGirl1 = new SelectGirl([&apos;小A&apos;,&apos;小B&apos;,&apos;小C&apos;]);</span><br><span class="line">const selectGirl2 = new SelectGirl([1,2,3]);</span><br></pre></td></tr></table></figure>
<h1 id="搭建浏览器开发环境"><a href="#搭建浏览器开发环境" class="headerlink" title="搭建浏览器开发环境"></a>搭建浏览器开发环境</h1><ol>
<li>建立好文件夹后，打开 <code>VSCode</code>，把文件夹拉到编辑器当中，然后打开终端，运行<code>npm init -y</code>,创建<code>package.json</code>文件。</li>
<li>生成文件后，我们接着在终端中运行<code>tsc -init</code>,生成<code>tsconfig.json</code>文件。</li>
<li>新建<code>src</code>和<code>build</code>文件夹，再建一个<code>index.html</code>文件。</li>
<li>在<code>src</code>目录下，新建一个<code>page.ts</code>文件，这就是我们要编写的<code>ts</code>文件了。</li>
<li>配置<code>tsconfig.json</code>文件，设置<code>outDir</code>和<code>rootDir</code>(在 15 行左右)，也就是设置需要编译的文件目录，和编译好的文件目录。</li>
<li>然后编写<code>index.html</code>，引入<code>&lt;script src=&quot;./build/page.js&quot;&gt;&lt;/script&gt;</code>,当让我们现在还没有<code>page.js</code>文件。</li>
<li>编写<code>page.ts</code>文件，加入一句输出<code>console.log(&#39;jspang.com&#39;)</code>,再在控制台输入<code>tsc</code>,就会生成<code>page.js</code>文件</li>
<li>再到浏览器中查看<code>index.html</code>文件，如果按<code>F12</code>可以看到<code>jspang.com</code>，说明我们的搭建正常了。</li>
</ol>
<h1 id="命名空间namespace"><a href="#命名空间namespace" class="headerlink" title="命名空间namespace"></a>命名空间namespace</h1><h2 id="当不使用命名空间时"><a href="#当不使用命名空间时" class="headerlink" title="当不使用命名空间时"></a>当不使用命名空间时</h2><p>在page.ts文件里，写出下面的代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">class Header &#123;</span><br><span class="line">  constructor() &#123;</span><br><span class="line">    const elem = document.createElement(&quot;div&quot;);</span><br><span class="line">    elem.innerText = &quot;This is Header&quot;;</span><br><span class="line">    document.body.appendChild(elem);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Content &#123;</span><br><span class="line">  constructor() &#123;</span><br><span class="line">    const elem = document.createElement(&quot;div&quot;);</span><br><span class="line">    elem.innerText = &quot;This is Content&quot;;</span><br><span class="line">    document.body.appendChild(elem);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Footer &#123;</span><br><span class="line">  constructor() &#123;</span><br><span class="line">    const elem = document.createElement(&quot;div&quot;);</span><br><span class="line">    elem.innerText = &quot;This is Footer&quot;;</span><br><span class="line">    document.body.appendChild(elem);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Page &#123;</span><br><span class="line">  constructor() &#123;</span><br><span class="line">    new Header();</span><br><span class="line">    new Content();</span><br><span class="line">    new Footer();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>写完后我们用<code>tsc</code>进行编译一次，然后修改<code>index.html</code>文件，在<code>&lt;body&gt;</code>标签里引入<code>&lt;script&gt;</code>标签，并实例化<code>Page</code>，代码如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;script&gt;new Page();&lt;/script&gt;</span><br><span class="line">&lt;/body&gt;</span><br></pre></td></tr></table></figure>
<p>此时控制台(Console)中，分别输入<code>Header</code>、<code>Content</code>、<code>Footer</code>和<code>Page</code>都时可以拿到对应的变量的,说明他们全都是全局变量。</p>
<h2 id="使用命名空间"><a href="#使用命名空间" class="headerlink" title="使用命名空间"></a>使用命名空间</h2><p>用namespace和export导出，用Home.page()实例化。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">namespace Home &#123;</span><br><span class="line">  class Header &#123;</span><br><span class="line">    constructor() &#123;</span><br><span class="line">      const elem = document.createElement(&quot;div&quot;);</span><br><span class="line">      elem.innerText = &quot;This is Header&quot;;</span><br><span class="line">      document.body.appendChild(elem);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  class Content &#123;</span><br><span class="line">    constructor() &#123;</span><br><span class="line">      const elem = document.createElement(&quot;div&quot;);</span><br><span class="line">      elem.innerText = &quot;This is Content&quot;;</span><br><span class="line">      document.body.appendChild(elem);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  class Footer &#123;</span><br><span class="line">    constructor() &#123;</span><br><span class="line">      const elem = document.createElement(&quot;div&quot;);</span><br><span class="line">      elem.innerText = &quot;This is Footer&quot;;</span><br><span class="line">      document.body.appendChild(elem);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  export class Page &#123;</span><br><span class="line">    constructor() &#123;</span><br><span class="line">      new Header();</span><br><span class="line">      new Content();</span><br><span class="line">      new Footer();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">new Home.Page();</span><br></pre></td></tr></table></figure>
<h2 id="组件化"><a href="#组件化" class="headerlink" title="组件化"></a>组件化</h2><p>新建一个<code>components.ts</code>文件作为组件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">namespace Components &#123;</span><br><span class="line">  export class Header1 &#123;</span><br><span class="line">    constructor() &#123;</span><br><span class="line">      const elem = document.createElement(&quot;div&quot;);</span><br><span class="line">      elem.innerText = &quot;A&quot;;</span><br><span class="line">      document.body.appendChild(elem);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在<code>page.ts</code>里面使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">namespace Home &#123;</span><br><span class="line">  export class Page &#123;</span><br><span class="line">    constructor() &#123;</span><br><span class="line">      new Components.Header1();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在<code>index.html</code>中引用新编辑的文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=&quot;./build/page.js&quot;&gt;&lt;/script&gt;</span><br><span class="line">&lt;script src=&quot;./build/components.js&quot;&gt;&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="多文件编译成一个文件"><a href="#多文件编译成一个文件" class="headerlink" title="多文件编译成一个文件"></a>多文件编译成一个文件</h2><p>将<code>tsconfig.json</code>中的<code>&quot;module&quot;:&quot;commonjs&quot;</code>改为<code>&quot;module&quot;:&quot;amd&quot;</code>；<br>然后修改<code>outFile</code>配置项。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;outFile&quot;: &quot;./build/page.js&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此时<code>index.html</code>文件不引用<code>component.js</code>也可以正常使用了。</p>
<h2 id="子命名空间"><a href="#子命名空间" class="headerlink" title="子命名空间"></a>子命名空间</h2><p>在<code>Components.ts</code>文件下编写：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">namespace Components &#123;</span><br><span class="line">  export namespace SubComponents &#123;</span><br><span class="line">    export class Test &#123;&#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  //someting ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>使用：<code>Components.SubComponents.Test</code></p>
<h1 id="使用import和export"><a href="#使用import和export" class="headerlink" title="使用import和export"></a>使用import和export</h1><p>修改<code>compontens.ts</code>，删除<code>namespace</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">export class Header1 &#123;</span><br><span class="line">    constructor() &#123;</span><br><span class="line">    const elem = document.createElement(&quot;div&quot;);</span><br><span class="line">    elem.innerText = &quot;A&quot;;</span><br><span class="line">    document.body.appendChild(elem);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export class Content1 &#123;</span><br><span class="line">    constructor() &#123;</span><br><span class="line">    const elem = document.createElement(&quot;div&quot;);</span><br><span class="line">    elem.innerText = &quot;B&quot;;</span><br><span class="line">    document.body.appendChild(elem);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="直接使用"><a href="#直接使用" class="headerlink" title="直接使用"></a>直接使用</h2><p><code>page.ts</code>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">import &#123; Header1, Content1 &#125; from &quot;./components&quot;;</span><br><span class="line">export class Page &#123;</span><br><span class="line">  constructor() &#123;</span><br><span class="line">    new Header1();</span><br><span class="line">    new Content1();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>html</code>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;new Home.Page();&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="使用require"><a href="#使用require" class="headerlink" title="使用require"></a>使用require</h2><p><strong>引入：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.js&quot;&gt;&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<p><strong>使用<code>default</code>关键字：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">import &#123; Header1, Content1 &#125; from &quot;./components&quot;;</span><br><span class="line">export default class Page &#123;</span><br><span class="line">  constructor() &#123;</span><br><span class="line">    new Header1();</span><br><span class="line">    new Content1();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>使用：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">    require([&apos;page&apos;], function(page)&#123;</span><br><span class="line">        new page.default();</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h1 id="使用parcel打包"><a href="#使用parcel打包" class="headerlink" title="使用parcel打包"></a>使用parcel打包</h1><p>安装</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add --dev parcel@next</span><br></pre></td></tr></table></figure>
<p>修改<code>package.json</code>里边的代码。</p>
<p>{</p>
<p>  “scripts”: {<br>    “test”: “parcel ./src/index.html”<br>  },<br>}</p>
<p>输入<code>yarn test</code>，打包出地址：<code>http://localhost:1234</code></p>
<h1 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h1><p>学习开始于2020-09-01，结束于2020-10-26，耗时55天。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/08/28/List去重统计的算法/" rel="next" title="List去重统计的算法">
                <i class="fa fa-chevron-left"></i> List去重统计的算法
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/09/08/echarts超全超详情配置项/" rel="prev" title="echarts超全超详情配置项">
                echarts超全超详情配置项 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zODk2OC8xNTQ5Ng=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="https://upload.jianshu.io/users/upload_avatars/830956/debe5156eed7?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240" alt="彭丹丹">
            
              <p class="site-author-name" itemprop="name">彭丹丹</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">183</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#安装及使用"><span class="nav-number">1.</span> <span class="nav-text">安装及使用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#安装"><span class="nav-number">1.1.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用"><span class="nav-number">1.2.</span> <span class="nav-text">使用</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#静态类型"><span class="nav-number">2.</span> <span class="nav-text">静态类型</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#基本使用"><span class="nav-number">2.1.</span> <span class="nav-text">基本使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#自定义静态类型"><span class="nav-number">2.2.</span> <span class="nav-text">自定义静态类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基础静态类型和对象类型"><span class="nav-number">2.3.</span> <span class="nav-text">基础静态类型和对象类型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#静态类型-1"><span class="nav-number">2.3.1.</span> <span class="nav-text">静态类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#对象类型"><span class="nav-number">2.3.2.</span> <span class="nav-text">对象类型</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#类型注释和类型推断"><span class="nav-number">3.</span> <span class="nav-text">类型注释和类型推断</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#类型注释"><span class="nav-number">3.1.</span> <span class="nav-text">类型注释</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#类型推断"><span class="nav-number">3.2.</span> <span class="nav-text">类型推断</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#函数参数和返回类型定义"><span class="nav-number">4.</span> <span class="nav-text">函数参数和返回类型定义</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#类型定义"><span class="nav-number">4.1.</span> <span class="nav-text">类型定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数参数为对象（解构）时"><span class="nav-number">4.2.</span> <span class="nav-text">函数参数为对象（解构）时</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#数组"><span class="nav-number">5.</span> <span class="nav-text">数组</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#各种类型"><span class="nav-number">5.1.</span> <span class="nav-text">各种类型</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#元组"><span class="nav-number">6.</span> <span class="nav-text">元组</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#接口"><span class="nav-number">7.</span> <span class="nav-text">接口</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#定义接口"><span class="nav-number">7.1.</span> <span class="nav-text">定义接口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#接口-interface-和类型别名-type-的区别"><span class="nav-number">7.2.</span> <span class="nav-text">接口(interface)和类型别名(type)的区别</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#接口定义非必填值"><span class="nav-number">7.3.</span> <span class="nav-text">接口定义非必填值?:</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#接口允许加入任意值-anyname-string-any"><span class="nav-number">7.4.</span> <span class="nav-text">接口允许加入任意值[anyname:string]:any;</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#接口里定义方法"><span class="nav-number">7.5.</span> <span class="nav-text">接口里定义方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#类实现接口"><span class="nav-number">7.6.</span> <span class="nav-text">类实现接口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#接口继承接口"><span class="nav-number">7.7.</span> <span class="nav-text">接口继承接口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#自我总结"><span class="nav-number">7.8.</span> <span class="nav-text">自我总结</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#类"><span class="nav-number">8.</span> <span class="nav-text">类</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#继承与重写"><span class="nav-number">8.1.</span> <span class="nav-text">继承与重写</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#访问类型"><span class="nav-number">8.2.</span> <span class="nav-text">访问类型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#public"><span class="nav-number">8.2.1.</span> <span class="nav-text">public</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#private"><span class="nav-number">8.2.2.</span> <span class="nav-text">private</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#protected"><span class="nav-number">8.2.3.</span> <span class="nav-text">protected</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#对比表"><span class="nav-number">8.2.4.</span> <span class="nav-text">对比表</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#构造函数"><span class="nav-number">8.3.</span> <span class="nav-text">构造函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#构造函数及继承"><span class="nav-number">8.3.1.</span> <span class="nav-text">构造函数及继承</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#get和set"><span class="nav-number">8.4.</span> <span class="nav-text">get和set</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#static装饰符"><span class="nav-number">8.5.</span> <span class="nav-text">static装饰符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#只读属性readonly"><span class="nav-number">8.6.</span> <span class="nav-text">只读属性readonly</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#抽象类"><span class="nav-number">8.7.</span> <span class="nav-text">抽象类</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#不加abstract时"><span class="nav-number">8.7.1.</span> <span class="nav-text">不加abstract时</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#加abstract关键字时"><span class="nav-number">8.7.2.</span> <span class="nav-text">加abstract关键字时</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多态"><span class="nav-number">8.7.3.</span> <span class="nav-text">多态</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#配置项tsconfig-json"><span class="nav-number">9.</span> <span class="nav-text">配置项tsconfig.json</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#include、exclude、files"><span class="nav-number">9.1.</span> <span class="nav-text">include、exclude、files</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#compilerOptions配置"><span class="nav-number">9.2.</span> <span class="nav-text">compilerOptions配置</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#联合类型和类型保护"><span class="nav-number">10.</span> <span class="nav-text">联合类型和类型保护</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#联合类型"><span class="nav-number">10.1.</span> <span class="nav-text">联合类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#类型保护-类型断言"><span class="nav-number">10.2.</span> <span class="nav-text">类型保护-类型断言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#类型保护-in语法"><span class="nav-number">10.3.</span> <span class="nav-text">类型保护-in语法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#断言和in的实例"><span class="nav-number">10.4.</span> <span class="nav-text">断言和in的实例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#类型保护-typeof语法"><span class="nav-number">10.5.</span> <span class="nav-text">类型保护-typeof语法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#类型保护-instanceof语法"><span class="nav-number">10.6.</span> <span class="nav-text">类型保护-instanceof语法</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#枚举"><span class="nav-number">11.</span> <span class="nav-text">枚举</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#泛型"><span class="nav-number">12.</span> <span class="nav-text">泛型</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#在方法中使用"><span class="nav-number">12.1.</span> <span class="nav-text">在方法中使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#在类中使用"><span class="nav-number">12.2.</span> <span class="nav-text">在类中使用</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#搭建浏览器开发环境"><span class="nav-number">13.</span> <span class="nav-text">搭建浏览器开发环境</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#命名空间namespace"><span class="nav-number">14.</span> <span class="nav-text">命名空间namespace</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#当不使用命名空间时"><span class="nav-number">14.1.</span> <span class="nav-text">当不使用命名空间时</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用命名空间"><span class="nav-number">14.2.</span> <span class="nav-text">使用命名空间</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#组件化"><span class="nav-number">14.3.</span> <span class="nav-text">组件化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#多文件编译成一个文件"><span class="nav-number">14.4.</span> <span class="nav-text">多文件编译成一个文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#子命名空间"><span class="nav-number">14.5.</span> <span class="nav-text">子命名空间</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#使用import和export"><span class="nav-number">15.</span> <span class="nav-text">使用import和export</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#直接使用"><span class="nav-number">15.1.</span> <span class="nav-text">直接使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用require"><span class="nav-number">15.2.</span> <span class="nav-text">使用require</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#使用parcel打包"><span class="nav-number">16.</span> <span class="nav-text">使用parcel打包</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#结语"><span class="nav-number">17.</span> <span class="nav-text">结语</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">彭丹丹</span>

  
</div>


	<div class="powered-by">
	<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
	  本站访客数:<span id="busuanzi_value_site_uv"></span>
	</span>
	</div>
  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  

  

  

  

  undefined
  
  <!-- 页面爆炸效果 -->
  
<script src="/live2dw/lib/L2Dwidget.min.js?0c58a1486de42ac6cc1c59c7d98ae887"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/tororo.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true}});</script></body>
</html>
